package com.kitty.game.pet.service;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.difu.service.RespGongShengNotify;
import com.kitty.game.pet.message.ReqPetEndGongsheng;
import com.kitty.game.pet.message.ReqPetGongsheng;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.team.message.RespMsg;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import org.apache.commons.lang3.StringUtils;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class PetGongShengService {
    @Autowired
    protected PetService petService;
    public void startGongSheng(IoSession session, ReqPetGongsheng reqPetGongsheng){
        Role role = SessionUtils.getRoleBySession(session);
        // 副宠物
        int zhuPetNo = reqPetGongsheng.getPos();
        // 主宠物
        int fuPetNo = reqPetGongsheng.getType();

        Pet fuPet = null;
        Pet zhuPet = null;
        Map<Integer, Pet> allPets = role.getPetBox().getAllPets();
        for (Map.Entry<Integer, Pet> entry : allPets.entrySet()) {
            System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
            Pet pet = entry.getValue();
            if(pet.getPosition() == zhuPetNo) {
                zhuPet = pet;
            }
            if(pet.getPosition() == fuPetNo) {
                fuPet = pet;
            }
        }
        if(zhuPet != null && fuPet != null){
            if (StringUtils.isNotBlank(zhuPet.getGongSheng_pet_id()) || StringUtils.isNotBlank(fuPet.getGongSheng_pet_id())) {
                MessagePusher.pushMessage(role, new RespMsg("该宠物已经共生于其它鬼宠或该鬼宠已经共生于其它宠物。"));
                return;
            }
            HashMap<String, Integer> fuGongShengEffect = fuPet.getGongShengEffect();
            HashMap<String, Integer> zhuGongShengEffect = zhuPet.getGongShengEffect();
            int liftFu = fuPet.getLifeEffect();
            int manaFu = fuPet.getManaEffect();
            int phyFu = fuPet.getPhyEffect();
            int magFu = fuPet.getMagEffect();
            int speedFu = fuPet.getSpeedEffect();

            int liftZhu = zhuPet.getLifeEffect();
            int manaZhu = zhuPet.getManaEffect();
            int phyZhu = zhuPet.getPhyEffect();
            int magZhu = zhuPet.getMagEffect();
            int speedZhu = zhuPet.getSpeedEffect();


            float aloguZ = alogGongSheng(zhuPet);
            fuGongShengEffect.put("life",(int)(liftFu*aloguZ));
            fuGongShengEffect.put("mana",(int)(manaFu*aloguZ));
            fuGongShengEffect.put("phy",(int)(phyFu*aloguZ));
            fuGongShengEffect.put("mag",(int)(magFu*aloguZ));
            fuGongShengEffect.put("speed",(int)(speedFu*aloguZ));
            fuPet.setGongSheng_pet_id(String.valueOf(zhuPet.getId()));

            float alogF = alogGongSheng(fuPet);
            zhuGongShengEffect.put("life",(int)(liftZhu*alogF));
            zhuGongShengEffect.put("mana",(int)(manaZhu*alogF));
            zhuGongShengEffect.put("phy",(int)(phyZhu*alogF));
            zhuGongShengEffect.put("mag",(int)(magZhu*alogF));
            zhuGongShengEffect.put("speed",(int)(speedZhu*alogF));
//            fuGongShengEffect.put("life",(int)Math.floor(liftZhu*0.2));
//            fuGongShengEffect.put("mana",(int)Math.floor(manaZhu*0.2));
//            fuGongShengEffect.put("phy",(int)Math.floor(phyZhu*0.2));
//            fuGongShengEffect.put("mag",(int)Math.floor(magZhu*0.2));
//            fuGongShengEffect.put("speed",(int)Math.floor(speedZhu*0.2));
//            fuPet.setGongSheng_pet_id(String.valueOf(zhuPet.getId()));
//
//            zhuGongShengEffect.put("life",(int)Math.floor(liftFu*0.2));
//            zhuGongShengEffect.put("mana",(int)Math.floor(manaFu*0.2));
//            zhuGongShengEffect.put("phy",(int)Math.floor(phyFu*0.2));
//            zhuGongShengEffect.put("mag",(int)Math.floor(magFu*0.2));
//            zhuGongShengEffect.put("speed",(int)Math.floor(speedFu*0.2));
            zhuPet.setGongSheng_pet_id(String.valueOf(fuPet.getId()));
            petService.refreshPetInfo(role, zhuPet);
            petService.refreshPetInfo(role, fuPet);
            RespGongShengNotify respGongShengNotify = new RespGongShengNotify();
            respGongShengNotify.setPet1((short)zhuPetNo);
            respGongShengNotify.setPet2((short)fuPetNo);
            respGongShengNotify.setA((byte)1);
            SpringUtils.getBean(RoleService.class).addAchieveScore(role,"201001");
            MessagePusher.pushMessage(session, respGongShengNotify);
        }
    }

    public void endGongSheng(IoSession session, ReqPetEndGongsheng reqPetEndGongsheng){
        int pos = reqPetEndGongsheng.getPos();
        Role role = SessionUtils.getRoleBySession(session);
        Pet zhuPet = null;
        String gongShengID = "";
        Map<Integer, Pet> allPets = role.getPetBox().getAllPets();
        for (Map.Entry<Integer, Pet> entry : allPets.entrySet()) {
            Pet pet = entry.getValue();
            if(pet.getPosition() == pos) {
                zhuPet = pet;
                gongShengID = pet.getGongSheng_pet_id();
                break;
            }
        }
        if(StringUtils.isNoneEmpty(gongShengID) && zhuPet != null){
            for (Map.Entry<Integer, Pet> entry : allPets.entrySet()) {
                Pet fuPet = entry.getValue();
                if(gongShengID.equals(String.valueOf(fuPet.getId()))) {
                    HashMap<String, Integer> fuGongShengEffect = fuPet.getGongShengEffect();
                    fuGongShengEffect.put("life",(int)0);
                    fuGongShengEffect.put("mana",(int)0);
                    fuGongShengEffect.put("phy",(int)0);
                    fuGongShengEffect.put("mag",(int)0);
                    fuGongShengEffect.put("speed",(int)0);
                    fuPet.setGongSheng_pet_id("");

                    HashMap<String, Integer> zhuGongShengEffect = zhuPet.getGongShengEffect();
                    zhuGongShengEffect.put("life",(int)0);
                    zhuGongShengEffect.put("mana",(int)0);
                    zhuGongShengEffect.put("phy",(int)0);
                    zhuGongShengEffect.put("mag",(int)0);
                    zhuGongShengEffect.put("speed",(int)0);
                    zhuPet.setGongSheng_pet_id("");

                    petService.refreshPetInfo(role, zhuPet);
                    petService.refreshPetInfo(role, fuPet);
                    break;
                }
            }
        }

    }
    public static  float  alogGongSheng(Pet pet2){
        float alogNum = 0.1f;
        if(pet2.getType() == 3){
            alogNum += 0.02;
        }
        if(pet2.getType() == 4){
            alogNum += 0.04;
        }
        if(pet2.getType() == 6){
            alogNum += 0.02;
        }
        if(pet2.getType() == 7){
            alogNum += 0.04;
        }
        if(pet2.getType() == 8){
            alogNum += 0.06;
        }
        if(pet2.getType() == 6 || pet2.getType() == 7 || pet2.getType() == 8){
            int peiYuanStage = pet2.getPeiyuan_data_stage();
            alogNum = alogNum + (peiYuanStage*0.01f);
            int ningShenStage = pet2.getNingshenFieldAdds().size();
            if(ningShenStage>0){
                alogNum = alogNum + (ningShenStage*0.02f);
            }
        }else{
            int enchant = pet2.getEnchant();
            if(enchant == 2){
                alogNum = alogNum + 0.1f;
            }
            int eclosion_rank = pet2.getEclosion_rank();
            if(eclosion_rank>0){
                alogNum = alogNum + ( eclosion_rank * 0.1F);
            }
            int huanhua1 = pet2.getMorphEffect().getOrDefault("life_times", 0);
            int huanhua2 = pet2.getMorphEffect().getOrDefault("mana_times", 0);
            int huanhua3 = pet2.getMorphEffect().getOrDefault("speed_times", 0);
            int huanhua4 = pet2.getMorphEffect().getOrDefault("phy_times", 0);
            int huanhua5 = pet2.getMorphEffect().getOrDefault("mag_times", 0);
            int huanhua = huanhua1+huanhua2+huanhua3+huanhua4+huanhua5;
            if(huanhua > 0){
                alogNum = alogNum + ( huanhua * 0.033F);
            }
        }
        return alogNum;
    }
}
